from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

url = 'http://imooc.com'
driver = webdriver.Chrome()

driver.get(url)
print(1, driver.current_url)

sleep(3)
js_script = 'window.open("https://www.baidu.com")'
driver.execute_script(js_script)
print(2, driver.current_url)

sleep(3)
a_element = driver.find_element(By.XPATH, './/div[@class="system-class-show"]/a[@data-position="3"]')
a_element.click()
print(3, driver.current_url)

sleep(3)
js_script = 'window.open("{}")'.format(a_element.get_attribute('href'))
driver.execute_script(js_script)
print(4, driver.current_url)

print(driver.current_window_handle)
for index, window in enumerate(driver.window_handles, start=1):
    driver.switch_to.window(window)
    print('window:', driver.current_url, driver.current_window_handle)
    sleep(5)

print(driver.current_window_handle)

sleep(5)
driver.quit()


